import File from './File';
import Context from '../Context';
import fs = require('fs');
import { joinPath } from '../Utils';
import path = require('path');
import BaseSourceFile from './BaseSourceFile';

export default class ModuleFile extends BaseSourceFile {

    public static readonly NODE_MODULES_PATH = '/node_modules/';
    public readonly type = FileType.module;
    public readonly name: string;

    constructor(cx: Context, op: CreateFileOptions) {
        super(cx, op);
        let src = this.src as string;
        let [_, base, name] = src.match(/^(.*?\/node_modules\/([^/]*)\/).*$/)
            || [undefined, undefined, undefined];
        if (!base || !name) {
            throw new Error('file ' + this.src + ' is not a module file');
        }
        this.name = name;
        this.id = joinPath(
            ModuleFile.NODE_MODULES_PATH,
            name,
            path.relative(base, src)
        );
    }
}
